C/C++

推荐列表 站点导航

当前位置:首页 > 脚本编程 > C/C++ >

C++_浅谈C++中replace()方法,本文主要针对c++中常用replace函

来源:网络整理  作者:fen  发布时间:2020-12-25 07:02
浅谈C++中replace()方法,本文主要针对c++中常用replace函数用法给出九个样例程序:用法一:/IT之家 IT之家用str替换指定字符串从起...

/IT之家 IT之家用str替换指定字符串从起始位置pos开始长度为len的字符 IT之家string& replace (size_t pos, size_t len, const string& str); IT之家/ int main() { string line = "this@ is@ a test string!"; line = line.replace(line.find("@"), 1, ""); //从第一个@位置替换第一个@为空 cout << line << endl; return 0; }

运行结果:

用法二: 

/IT之家 IT之家用str替换 迭代器起始位置 和 结束位置 的字符 IT之家string& replace (const_iterator i1, const_iterator i2, const string& str); IT之家/ int main() { string line = "this@ is@ a test string!"; line = line.replace(line.begin(), line.begin()+6, ""); //用str替换从begin位置开始的6个字符 cout << line << endl; return 0; }

运行结果:

用法三: 

/IT之家 IT之家用substr的指定子串(给定起始位置和长度)替换从指定位置上的字符串 IT之家string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen); IT之家/ int main() { string line = "this@ is@ a test string!"; string substr = "12345"; line = line.replace(0, 5, substr, substr.find("1"), 3); //用substr的指定子串(从1位置数共3个字符)替换从0到5位置上的line cout << line << endl; return 0; }

运行结果:

用法四:string转charIT之家时编译器可能会报出警告,不建议这样做 

/IT之家 IT之家用str替换从指定位置0开始长度为5的字符串 IT之家string& replace(size_t pos, size_t len, const charIT之家 s); IT之家/ int main() { string line = "this@ is@ a test string!"; charIT之家 str = "12345"; line = line.replace(0, 5, str); //用str替换从指定位置0开始长度为5的字符串 cout << line << endl; return 0; }

运行结果:

用法五:string转charIT之家时编译器可能会报出警告,不建议这样做 

/IT之家 IT之家用str替换从指定迭代器位置的字符串 IT之家string& replace (const_iterator i1, const_iterator i2, const charIT之家 s); IT之家/ int main() { string line = "this@ is@ a test string!"; charIT之家 str = "12345"; line = line.replace(line.begin(), line.begin()+9, str); //用str替换从指定迭代器位置的字符串 cout << line << endl; return 0; }

运行结果:

用法六:string转charIT之家时编译器可能会报出警告,不建议这样做 

/IT之家 IT之家用s的前n个字符替换从开始位置pos长度为len的字符串 IT之家string& replace(size_t pos, size_t len, const charIT之家 s, size_t n); IT之家/ int main() { string line = "this@ is@ a test string!"; charIT之家 str = "12345"; line = line.replace(0, 9, str, 4); //用str的前4个字符替换从0位置开始长度为9的字符串 cout << line << endl; return 0; }

运行结果:

用法七:string转charIT之家时编译器可能会报出警告,不建议这样做 

/IT之家 IT之家用s的前n个字符替换指定迭代器位置(从i1到i2)的字符串 IT之家string& replace (const_iterator i1, const_iterator i2, const charIT之家 s, size_t n); IT之家/ int main() { string line = "this@ is@ a test string!"; charIT之家 str = "12345"; line = line.replace(line.begin(), line.begin()+9, str, 4); //用str的前4个字符替换指定迭代器位置的字符串 cout << line << endl; return 0; }

运行结果:

用法八: 

/IT之家 IT之家用重复n次的c字符替换从指定位置pos长度为len的内容 IT之家string& replace (size_t pos, size_t len, size_t n, char c); IT之家/ int main() { string line = "this@ is@ a test string!"; char c = '1'; line = line.replace(0, 9, 3, c); //用重复3次的c字符替换从指定位置0长度为9的内容 cout << line << endl; return 0; }

运行结果:

用法九: 

/IT之家 IT之家用重复n次的c字符替换从指定迭代器位置(从i1开始到结束)的内容 IT之家string& replace (const_iterator i1, const_iterator i2, size_t n, char c); IT之家/ int main() { string line = "this@ is@ a test string!"; char c = '1'; line = line.replace(line.begin(), line.begin()+9, 3, c); //用重复3次的c字符替换从指定迭代器位置的内容 cout << line << endl; return 0; }

运行结果:

注:所有使用迭代器类型的参数不限于string类型,可以为vector、list等其他类型迭代器。

相关热词: 方法 C++

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/jiaob/cjj/8822.shtml

最新文章
只需要在调用Ctrl+B编译后 只需要在调用Ctrl+B编译后

时间:2021-01-13

OpenGL超级宝典visual studio OpenGL超级宝典visual studio

时间:2021-01-04

Directx11 教程(2) 基本的wi Directx11 教程(2) 基本的wi

时间:2021-01-04

LeetCode11ContainerWithMostWate LeetCode11ContainerWithMostWate

时间:2021-01-04

C语言简单IT之家速成 C语言简单IT之家速成

时间:2020-12-27

三分钟了解Activity工作流 三分钟了解Activity工作流

时间:2020-12-27

编译器是如何实现32位整型 编译器是如何实现32位整型

时间:2020-12-27

C++中lower_bound函数和upper C++中lower_bound函数和upper

时间:2020-12-27

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

C++_浅谈C++中replace()方法,本文主要针对c++中常用replace函

2020-12-25 编辑:fen

/IT之家 IT之家用str替换指定字符串从起始位置pos开始长度为len的字符 IT之家string& replace (size_t pos, size_t len, const string& str); IT之家/ int main() { string line = "this@ is@ a test string!"; line = line.replace(line.find("@"), 1, ""); //从第一个@位置替换第一个@为空 cout << line << endl; return 0; }

运行结果:

用法二: 

/IT之家 IT之家用str替换 迭代器起始位置 和 结束位置 的字符 IT之家string& replace (const_iterator i1, const_iterator i2, const string& str); IT之家/ int main() { string line = "this@ is@ a test string!"; line = line.replace(line.begin(), line.begin()+6, ""); //用str替换从begin位置开始的6个字符 cout << line << endl; return 0; }

运行结果:

用法三: 

/IT之家 IT之家用substr的指定子串(给定起始位置和长度)替换从指定位置上的字符串 IT之家string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen); IT之家/ int main() { string line = "this@ is@ a test string!"; string substr = "12345"; line = line.replace(0, 5, substr, substr.find("1"), 3); //用substr的指定子串(从1位置数共3个字符)替换从0到5位置上的line cout << line << endl; return 0; }

运行结果:

用法四:string转charIT之家时编译器可能会报出警告,不建议这样做 

/IT之家 IT之家用str替换从指定位置0开始长度为5的字符串 IT之家string& replace(size_t pos, size_t len, const charIT之家 s); IT之家/ int main() { string line = "this@ is@ a test string!"; charIT之家 str = "12345"; line = line.replace(0, 5, str); //用str替换从指定位置0开始长度为5的字符串 cout << line << endl; return 0; }

运行结果:

用法五:string转charIT之家时编译器可能会报出警告,不建议这样做 

/IT之家 IT之家用str替换从指定迭代器位置的字符串 IT之家string& replace (const_iterator i1, const_iterator i2, const charIT之家 s); IT之家/ int main() { string line = "this@ is@ a test string!"; charIT之家 str = "12345"; line = line.replace(line.begin(), line.begin()+9, str); //用str替换从指定迭代器位置的字符串 cout << line << endl; return 0; }

运行结果:

用法六:string转charIT之家时编译器可能会报出警告,不建议这样做 

/IT之家 IT之家用s的前n个字符替换从开始位置pos长度为len的字符串 IT之家string& replace(size_t pos, size_t len, const charIT之家 s, size_t n); IT之家/ int main() { string line = "this@ is@ a test string!"; charIT之家 str = "12345"; line = line.replace(0, 9, str, 4); //用str的前4个字符替换从0位置开始长度为9的字符串 cout << line << endl; return 0; }

运行结果:

用法七:string转charIT之家时编译器可能会报出警告,不建议这样做 

/IT之家 IT之家用s的前n个字符替换指定迭代器位置(从i1到i2)的字符串 IT之家string& replace (const_iterator i1, const_iterator i2, const charIT之家 s, size_t n); IT之家/ int main() { string line = "this@ is@ a test string!"; charIT之家 str = "12345"; line = line.replace(line.begin(), line.begin()+9, str, 4); //用str的前4个字符替换指定迭代器位置的字符串 cout << line << endl; return 0; }

运行结果:

用法八: 

/IT之家 IT之家用重复n次的c字符替换从指定位置pos长度为len的内容 IT之家string& replace (size_t pos, size_t len, size_t n, char c); IT之家/ int main() { string line = "this@ is@ a test string!"; char c = '1'; line = line.replace(0, 9, 3, c); //用重复3次的c字符替换从指定位置0长度为9的内容 cout << line << endl; return 0; }

运行结果:

用法九: 

/IT之家 IT之家用重复n次的c字符替换从指定迭代器位置(从i1开始到结束)的内容 IT之家string& replace (const_iterator i1, const_iterator i2, size_t n, char c); IT之家/ int main() { string line = "this@ is@ a test string!"; char c = '1'; line = line.replace(line.begin(), line.begin()+9, 3, c); //用重复3次的c字符替换从指定迭代器位置的内容 cout << line << endl; return 0; }

运行结果:

注:所有使用迭代器类型的参数不限于string类型,可以为vector、list等其他类型迭代器。

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/jiaob/cjj/8822.shtml

相关文章

风云图片

推荐阅读

返回C/C++频道首页